/*
 * @Author: NtyAaron
 * @Date: 2021-04-12 11:13:45
 * @LastEditors: NtyAaron
 * @LastEditTime: 2021-06-09 17:21:55
 * @Description: file content
 * @FilePath: \dwq\pages\my\myCollect\myCollect.js
 */
import { getCollectList, delCollect } from '../../../api/article/article'
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog'
import { _t } from '../../../i18n/base'

Page({
  data: {
    list: [],
    messagePath: '/pages/my/myCollect/myCollect',
    messageImg: '/images/logo新.png',
  },
  // 获取收藏列表
  async getCollectList() {
    let userInfo = wx.getStorageSync('userInfo')
    if (userInfo) {
      let user = JSON.parse(userInfo)
      let { result } = await getCollectList({ userId: user.userId })
      console.log(result)
      this.setData({
        list: result,
      })
    }
  },
  // 取消收藏
  async onCloseCollect(e) {
    const { instance } = e.detail
    let id = e.currentTarget.dataset.id
    let { result } = await delCollect({ ids: id })
    if (result) {
      instance.close()
      this.getCollectList()
    }
  },
  vantDialog(e) {
    Dialog.confirm({
      title: this.data.lang['查看内容'],
      message: this.data.lang['跳转内容链接'],
      confirmButtonText: this.data.lang['确定'],
      cancelButtonText: this.data.lang['取消'],
      confirmButtonOpenType: 'contact',
      sendMessageTitle: e.title,
      sendMessagePath: `${this.data.messagePath}?${e.id},${e.type}`,
      sendMessageImg: this.data.messageImg,
      showMessageCard: true,
    })
      .then(async () => {
        setTimeout(() => {
          console.log(12)
        })
      })
      .catch(() => {})
  },
  // 跳转客服详情
  toCustomerServiceHandle(e) {
    let detail = e.currentTarget.dataset.info
    let vantObj = {
      type: detail.type,
      id: detail.articleId,
      title: detail.title,
    }
    this.vantDialog(vantObj)
  },
  onLoad() {
    const lang = _t()
    this.setData({
      lang,
    })
    wx.setNavigationBarTitle({
      title: lang['我的收藏'],
    })
    this.getCollectList()
  },
})
